package com.vn.bklegends.xonefm;

import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class PlayActivity extends Activity {

    public static News news;
    private boolean playing = false;
    private SeekBar seekBar;
    private TextView timer, name;
    private MediaPlayer mp;
    private ImageView play, next, previous;
    private int maxTime;
    
    private String TAG = "PlayActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.play);
	init();
	new Thread(new Runnable() {
	    
	    @Override
	    public void run() {
		prepare();
	    }
	}).start();
    }

    private void init() {
	play = (ImageView) findViewById(R.id.img_play);
	mp = new MediaPlayer();
	name = (TextView) findViewById(R.id.tv_name);
	name.setText(news.getName());

	timer = (TextView) findViewById(R.id.tv_time);
	seekBar = (SeekBar) findViewById(R.id.seek_bar);
	seekBar.setMax(100);

	play.setOnClickListener(playListener);

	seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

	    @Override
	    public void onStopTrackingTouch(SeekBar seekBar) {

	    }

	    @Override
	    public void onStartTrackingTouch(SeekBar seekBar) {

	    }

	    @Override
	    public void onProgressChanged(SeekBar seekBar, int progress,
		    boolean fromUser) {
		if (playing && fromUser) {
		    mp.seekTo(maxTime * progress / 100);
		}
	    }
	});
	
	mp.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
	    
	    @Override
	    public void onBufferingUpdate(MediaPlayer mp, int percent) {
		seekBar.setSecondaryProgress(percent);		
	    }
	});

    }

    private void prepare() {
	try {

	    mp.setDataSource(news.getUri());
	    mp.prepare();

	} catch (IOException e) {
	    Log.e(TAG, "prepare() failed");
	} finally {
	    runOnUiThread(new Runnable() {
	        
	        @Override
	        public void run() {
	            maxTime = mp.getDuration();
		    setTime(maxTime);
	        }
	    });
	}

    }

    private OnClickListener playListener = new OnClickListener() {

	@Override
	public void onClick(View v) {
	    if (!playing) {
		new Thread(new Runnable() {

		    @Override
		    public void run() {
			
			mp.start();
			runOnUiThread(new Runnable() {

			    @Override
			    public void run() {
				playing = true;
				play.setImageResource(android.R.drawable.ic_media_pause);
				new Thread(new Runnable() {
				    
				    @Override
				    public void run() {
					progressUpdater();
				    }
				}).start();
			    }
			});
			
			
		    }
		}).start();
	    } else {
		playing = false;
		play.setImageResource(android.R.drawable.ic_media_play);
		mp.pause();
	    }
	}
    };

    private void progressUpdater() {

	
	while (playing) {
	    final int position = mp.getCurrentPosition();
	    Log.i(TAG, "Position"+String.valueOf(position));
	    seekBar.setProgress(position * 100 / maxTime);
	    runOnUiThread(new Runnable() {

		@Override
		public void run() {
		    setTime(maxTime - position);
		}
	    });
	}

    }

    private void setTime(int time) {
	time = time / 1000;
	int p = time / 60;
	int second = time - p * 60;
	timer.setText(String.format("%2d : %2d", p, second));
    }

    @Override
    protected void onDestroy() {
	super.onDestroy();
	mp.stop();
    }

}
